<?php

namespace App\Http\Controllers\V1\Api;

use App\Http\Requests\UserRequest;
use App\Models\V1\Admins;
use App\Models\V1\Advert;
use App\Models\V1\Bank;
use App\Models\V1\BankClass;
use App\Models\V1\DeveloperOperationLog;
use App\Models\V1\Evaluating;
use App\Models\V1\GameClass;
use App\Models\V1\GameType;
use App\Models\V1\GameVersion;
use App\Models\V1\News;
use App\Models\V1\SysPermission;
use App\Models\V1\SysRole;
use App\Models\V1\UpdateLog;
use App\Models\V1\Users;
use Earnp\GoogleAuthenticator\GoogleAuthenticator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class EvaluatingController extends BaseController
{

    /**
     * 申请评测
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function Add(Request $request)
    {


        try {
            $evaluating_model = new Evaluating();
            $ulid = $request->input('ulid', "");
            $logo = GameVersion::query()->where('ulid', $request->input('game_id'))->value('logo');
            if ($ulid) {
                $gameimg = json_encode(explode(',', $request->input('gameimg', "")));
                $evaluating_model->where('ulid', $ulid)
                    ->update([
                        'user' => $request->input('user', ""),
                        'user_name' => $request->input('user_name', ""),
                        'settlement_interval' => $request->input('settlement_interval', ""),
                        'game_id' => $request->input('game_id', ""),//游戏id
                        'title' => $request->input('title', ""),//游戏名
                        'logo' => $logo,
                        'type' => $request->input('type', ""),//版本类型
                        'class' => $request->input('class', ""),//版本类型
                        'system' => $request->input('system', ""),//平台
                        'platform' => 'bq01H7ESMBTPNK0TTTX49JNP336E',
                        'content' => $request->input('content', ""),//版本说明
                        'ratio' => $request->input('ratio', ""),//充值比例
                        'gameimg' => $gameimg,//游戏截图
                        'apkadd' => $request->input('apkadd', ""),//测试APK下载地址
                        'pcadd' => $request->input('pcadd', ""),//测试PC下载地址
                        'settlement_ratio' => $request->input('settlement_ratio', ""),//结算比例
                        'channel_state' => 1,
                    ]);
            } else {
                $evaluating_model->create([
                    'ulid' => time(),
                    'user' => $request->input('user', ""),
                    'user_name' => $request->input('user_name', ""),
                    'settlement_interval' => $request->input('settlement_interval', ""),
                    'game_id' => $request->input('game_id', ""),//游戏id
                    'type' => $request->input('type', ""),
                    'class' => $request->input('class', ""),
                    'title' => $request->input('title', ""),
                    'logo' => $logo,
                    'content' => $request->input('content', ""),
                    'ratio' => $request->input('ratio', ""),
                    'gameimg' => $request->input('gameimg', ""),
                    'apkadd' => $request->input('apkadd', ""),
                    'system' => $request->input('system', ""),
                    'platform' => 'bq01H7ESMBTPNK0TTTX49JNP336E',
                    'settlement_ratio' => $request->input('settlement_ratio', ""),//结算比例
                    'pcadd' => $request->input('pcadd', ""),
                ]);
            }

            // 添加开发者操作日志
            DeveloperOperationLog::query()->create([
                'user' => $request['user'],
                'ip' => $request->getClientIp(),
                'ulid'=>time(),
                'attributes' => !empty($request->all()) ? json_encode($request->all()) : null,
                'remark' => '申请评测',
            ]);

        } catch (\Exception $e) {

            return ajax_output(300, "数据异常:" . $e->getMessage());
        }
        return ajax_output(200, "成功");
    }


    /**
     * 版本类型
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function GameType(Request $request)
    {
        $pageSize = $request->input('pageSize', 10);
        $query = GameType::query();
        $data = $query->paginate($pageSize);
        return ajax_output(200, "成功", $data);
    }


    /**
     * 版本分类
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function GameClass(Request $request)
    {
        $pageSize = $request->input('pageSize', 10);
        $type = $request->input('type');
        $query = GameClass::query();
        $data = $query->where('type', $type)->paginate($pageSize);
        return ajax_output(200, "成功", $data);
    }


    /**
     * 申请渠道
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function AppChannel(Request $request)
    {

        $ulid = $request->input('ulid', "");
        $game_version_model = new GameVersion();

        $game_version_model->where('ulid', $ulid)->update([
            'channel_state' => 1,
        ]);

        // 添加开发者操作日志
        DeveloperOperationLog::query()->create([
            'user' => $request['user'],
            'ip' => $request->getClientIp(),
            'ulid'=>time(),
            'attributes' => !empty($request->all()) ? json_encode($request->all()) : null,
            'remark' => '申请渠道',
        ]);
        return ajax_output(200, "成功");


    }
}
